﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Text.RegularExpressions;
using System.IO.IsolatedStorage;
using System.Windows.Navigation;

namespace AllTalk
{
    public class LoginViewModel : ViewModelBase
    {
        private static string EmailPattern = @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
        
        private string _email;
        
        public string Email
        {
            get
            {
                return _email;
            }
            set
            {
                string input = value.Trim();
                ValidateEmail(input);
                if (!String.Equals(_email, input, StringComparison.OrdinalIgnoreCase))
                {
                    _email = input;
                    NotifyPropertyChanged("Email");
                }
            }
        }

        public string _status;

        public string Status
        {
            get
            {
                return _status;
            }
            set
            {
                if (_status != value)
                {
                    _status = value;
                    NotifyPropertyChanged("Status");
                }
            }
        }

        private static void ValidateEmail(string email)
        {
            if (String.IsNullOrEmpty(email))
            {
                throw new ArgumentException("Email address should not be empty.");
            }
            if (!Regex.IsMatch(email, EmailPattern, RegexOptions.IgnoreCase))
            {
                throw new ArgumentException("Invalid email address format.");
            }
        }

        private string _password;
        public string Password
        {
            get
            {
                return _password;
            }
            set
            {
                string input = value.Trim();
                if (String.IsNullOrEmpty(input))
                {
                    throw new ArgumentException("Password should not be empty.");
                }
                if (!String.Equals(_password, input, StringComparison.OrdinalIgnoreCase))
                {
                    _password = input;
                    NotifyPropertyChanged("Password");
                }
            }
        }
        private bool _autoSignIn;
        public bool AutoSignIn
        {
            get
            {
                return _autoSignIn;
            }
            set
            {
                if (_autoSignIn != value)
                {
                    _autoSignIn = value;
                    NotifyPropertyChanged("AutoSignIn");
                }
            }
        }

        public bool IsLoaded { get; private set; }

        public bool CanAutoSignIn 
        { 
            get 
            {
                return this.AutoSignIn &&
                    !String.IsNullOrEmpty(this.Email) &&
                    !String.IsNullOrEmpty(this.Password);
            }
        }

        public bool LoadData()
        {
            try
            {
                string email;
                string password;
                bool autoSignIn;
                if (IsolatedStorageSettings.ApplicationSettings.TryGetValue(StorageConstants.Email, out email))
                {
                    this.Email = email;
                }

                if(IsolatedStorageSettings.ApplicationSettings.TryGetValue(StorageConstants.AutoSignIn, out autoSignIn))
                {
                    this.AutoSignIn = autoSignIn;
                    if (this.AutoSignIn)
                    {
                        if(IsolatedStorageSettings.ApplicationSettings.TryGetValue(StorageConstants.Password, out password))
                        {
                            this.Password = password;
                        }
                    }
                }
            }
            catch (ArgumentException)
            {
                // Do nothing.
            }
            this.IsLoaded = true;
            return this.IsLoaded;
        }

        public void SaveData()
        {
            IsolatedStorageSettings.ApplicationSettings[StorageConstants.Email] = this.Email;
            if (this.AutoSignIn)
            {
                IsolatedStorageSettings.ApplicationSettings[StorageConstants.Password] = this.Password;
            }
            else
            {
                IsolatedStorageSettings.ApplicationSettings.Remove(StorageConstants.Password);
            }
            IsolatedStorageSettings.ApplicationSettings[StorageConstants.AutoSignIn] = this.AutoSignIn;
            IsolatedStorageSettings.ApplicationSettings.Save();
        }
    }
}
